"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const fs_promise_1 = __importDefault(require("fs-promise"));
const colors_console_1 = __importDefault(require("colors-console"));
const si = require('../../ext/systeminformation');
const sleep_1 = __importDefault(require("../lib/sleep"));
const RSATool_1 = __importDefault(require("../lib/RSATool"));
const getServerInfo_1 = __importDefault(require("../lib/System/getServerInfo"));
let configInit = false;
const defaultBaseConfig = {
    'name': '地理数据汇聚管理平台',
    'version': 'v3.5',
    'version_time': '2025-08-22',
    'clientCORS': true,
    'clientCORS_desc': '是否允许跨域请求主服务的API接口',
    'protocol': 'http',
    'protocol_desc': '启动本程序 HTTP 服务器的协议，http 或 https',
    'httpServerPort': 3000,
    'httpServerPort_desc': '启动本程序 HTTP 服务器，发布服务的端口号',
    'proxyProtocol': 'https',
    'proxyProtocol_desc': '经过网关、交换机、防火墙等一系列代理后，外部网络访问本程序 Web 服务的协议号',
    'proxyAfterProxy_Port': 3000,
    'proxyAfterProxy_Port_desc': '经过网关、交换机、防火墙等一系列代理后，外部网络访问本程序 HTTP 服务的端口号',
    'localIPAddress': '127.0.0.1',
    'localIPAddress_desc': '在局域网内网中，部署本程序宿主机的 IP 地址',
    'proxyAfterProxy_IPAddress': '127.0.0.1',
    'proxyAfterProxy_IPAddress_desc': '经过网关、交换机、防火墙等一系列代理后，外部网络访问本程序 HTTP 服务的 IP 地址',
    'normalDBExt': '.sqlite3',
    'normalDBExt_desc': '普通地理资源数据库文件扩展名，如：.sqlite3',
    'encryptDBExt': '.enDB',
    'encryptDBExt_desc': '加密地理资源库数据库文件扩展名，如：.enDB',
    'encryptDBCache': false,
    'encryptDBCache_desc': '是开启地理资源请求时，将结果数据保存并写入资源库的插件',
    'blackList': [],
    'blackList_desc': '黑名单列表，禁止访问本程序 HTTP 服务的客户端 IP 地址',
    'wwwPath': 'www',
    'MapBox': {
        'access_token': 'pk.eyJ1Ijoic3ZjLW9rdGEtbWFwYm94LXN0YWZmLWFjY2VzcyIsImEiOiJjbG5sMnExa3kxNTJtMmtsODJld24yNGJlIn0.RQ4CHchAYPJQZSiUJ0O3VQ',
        'header': {
            'Referer': 'https://geojson.io/'
        }
    },
    'TianDiTu': {
        'tk': ''
    },
    'CacheSave': {
        'CacheSaveFile': true,
        'CacheSaveDB': false
    },
    'userList': [
        {
            'userName': 'user_1',
            'nickname': '访客',
            'password': '123456',
            'role': 'client'
        },
        {
            'userName': 'admin',
            'nickname': '指挥员',
            'password': '123456',
            'role': 'manage'
        }
    ],
    'publicDer': '',
    'privateDer': '',
    'desc': '服务器端配置文件，具体字段的说明和填写规范，可查阅源码工程目录下的 ./src/com/configTool.ts 文件'
};
const defaultServiceConfig = {};
const configTool = {
    appBasePath: '',
    wwwPath: '',
    serverInfo: {
        'arch': null,
        'PCName': null,
        'memorySize': null,
        'operatingSystem': null,
        'CPU': null,
        'machineId': ''
    },
    baseConfig: defaultBaseConfig,
    serviceConfig: defaultServiceConfig,
    async editConfig(key, value) {
        await this.init();
        let result = false;
        if (typeof this.baseConfig[key] === typeof value) {
            this.baseConfig[key] = value;
            await fs_promise_1.default.writeJSON(path_1.default.join(this.appBasePath, '/conf/baseConfig.json'), this.baseConfig);
            result = true;
        }
        return result;
    },
    async saveServerConfig(json) {
        await fs_promise_1.default.writeJSON(path_1.default.join(this.appBasePath, '/conf/serviceConfig.json'), json);
        this.serviceConfig = json;
    },
    async init() {
        return new Promise(async (resolve, reject) => {
            if (!configInit) {
                configInit = true;
                let isDev = !__dirname.includes('snapshot');
                if (isDev) {
                    this.appBasePath = path_1.default.join(__dirname, '../../');
                }
                else {
                    this.appBasePath = path_1.default.dirname(process.execPath);
                }
                this.serverInfo = await (0, getServerInfo_1.default)();
                await fs_promise_1.default.ensureDir(path_1.default.join(this.appBasePath, '/conf'));
                await fs_promise_1.default.ensureDir(path_1.default.join(this.appBasePath, '/temp/fileOut'));
                await fs_promise_1.default.ensureDir(path_1.default.join(this.appBasePath, '/temp/fileDB'));
                await fs_promise_1.default.ensureDir(path_1.default.join(this.appBasePath, '/temp/fileDB/upload'));
                await fs_promise_1.default.ensureDir(path_1.default.join(this.appBasePath, '/temp/logs/httpServer'));
                await fs_promise_1.default.ensureDir(path_1.default.join(this.appBasePath, '/www'));
                await fs_promise_1.default.ensureDir(path_1.default.join(this.appBasePath, '/www/MapResources'));
                let configFile = path_1.default.join(this.appBasePath, '/conf/baseConfig.json');
                let serviceFile = path_1.default.join(this.appBasePath, '/conf/serviceConfig.json');
                await fs_promise_1.default.readJson(configFile).then((d) => {
                    if (d.version === defaultBaseConfig.version) {
                        this.baseConfig = d;
                    }
                    else {
                        console.log((0, colors_console_1.default)('red', '----|||配置文件版本过低，未生效！！请重置配置文件|||----'));
                        resolve(false);
                    }
                }).catch(() => {
                    this.baseConfig = defaultBaseConfig;
                    const { privateDer, publicDer } = RSATool_1.default.createKey(2048);
                    this.baseConfig.privateDer = privateDer;
                    this.baseConfig.publicDer = publicDer;
                    fs_promise_1.default.writeJSON(configFile, this.baseConfig);
                });
                await fs_promise_1.default.readJson(serviceFile).then((d) => {
                    this.serviceConfig = d;
                }).catch(() => {
                    fs_promise_1.default.writeJSON(serviceFile, this.serviceConfig);
                });
                if (this.baseConfig.localIPAddress === 'localhost') {
                    let networks = await si.networkInterfaces();
                    networks = networks.filter((item) => item.ip6subnet);
                    let defaultNet = networks.find((item) => item.default);
                    if (!defaultNet) {
                        defaultNet = networks[0];
                    }
                    this.baseConfig.localIPAddress = defaultNet?.ip4 || '127.0.0.1';
                }
                if (path_1.default.isAbsolute(this.baseConfig.wwwPath)) {
                    this.wwwPath = path_1.default.join(this.baseConfig.wwwPath);
                }
                else {
                    this.wwwPath = path_1.default.join(this.appBasePath, this.baseConfig.wwwPath);
                }
            }
            while (!configInit) {
                await (0, sleep_1.default)(50);
            }
            resolve(true);
        });
    },
    preservableONDB() {
        return this.baseConfig.CacheSave.CacheSaveDB;
    },
    preservableONFile() {
        return this.baseConfig.CacheSave.CacheSaveFile;
    }
};
exports.default = configTool;
